import { defineStore } from "pinia";
import { ref, watch } from "vue";
import { generateRoutes, Storage } from "../utils/common";
import router from "../router";

function initState() {
  return {
    isCollapse: false,
    isRefresh: false,
    tags: [
      {
        path: "/home",
        name: "home",
        label: "首页",
        icon: "home",
      },
    ],
    token: Storage.get("token") || "",
    routerList: Storage.get("routerList") || [],
    currentMenu: null,
    menuList: [],
    permissionList: JSON.parse(localStorage.getItem("authList")) || [],
  };
}

export const useAllDataStore = defineStore("allData", () => {
  const state = ref(initState());
  watch(
    state,
    (newObj) => {
      if (!newObj.token) return;
      Storage.set("token", newObj.token);
    },
    { deep: true }
  );

  //tag菜单选择
  function selectMenu(val) {
    if (val.name === "home") {
      state.value.currentMenu = null;
    } else {
      let index = state.value.tags.findIndex((item) => item.name === val.name);
      index === -1 ? state.value.tags.push(val) : "";
    }
  }
  //tag标签关闭
  function updateTags(tag) {
    let index = state.value.tags.findIndex((item) => item.name === tag.name);
    state.value.tags.splice(index, 1);
  }

  /*
   * 添加路由
   * @param {Array} routerList 路由列表
   */
  const updateMenuList = (routerList = []) => {
    return new Promise((resolve, reject) => {
      Storage.set("routerList", routerList);
      state.value.routerList = routerList;
      const gRoutes = generateRoutes(routerList);
      state.value.isRefresh = true;
      // const routers = router.getRoutes();
      // routers.forEach((item) => {
      //   if (
      //     item.name !== "main" ||
      //     item.name !== "login" ||
      //     item.name !== "404"
      //   ) {
      //   } else {
      //     router.removeRoute(item.name);
      //   }
      // });
      resolve(gRoutes);
    });
  };
  /*
   * 添加权限列表
   * @param {Array} authList 权限列表
   */
  const updatePermissionList = (authList = []) => {
    state.value.permissionList = authList;
    localStorage.setItem("authList", JSON.stringify(authList));
  };
  const setRoute = (routes = []) => {
    return new Promise((resolve, reject) => {
      routes.forEach((route) => {
        router.addRoute("main", route);
      });
      resolve(1);
    });
  };
  //store重置
  function clean() {
    // state.value.routerList.forEach((item) => {
    //   if (item) item();
    // });
    // Clear the routerList and token in the state
    state.value = {
      routerList: [],
      token: "",
      tags: [
        {
          path: "/home",
          name: "home",
          label: "首页",
          icon: "home",
        },
      ],
    };
    //清楚多个参数token,reoterList等
    localStorage.removeItem("routerList");
    localStorage.removeItem("token");
    localStorage.removeItem("authList");
    localStorage.removeItem("userInfo");
  }

  return {
    state,
    clean,
    selectMenu,
    updateTags,
    updateMenuList,
    updatePermissionList,
    setRoute,
  };
});
